//
//  RWCollectionTableViewCell.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/26.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "RWCollectionTableViewCell.h"

#import "RWGoodsModel.h"

@interface RWCollectionTableViewCell ()

@property (nonatomic, copy) RWGoodsModel *goodsModel;

@end

@implementation RWCollectionTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setCollectionCell];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        [self setCollectionCell];
    }
    return self;
}

- (void)loadDataWithModel:(id)model{

    _goodsModel = [RWGoodsModel mj_objectWithKeyValues:model];
    [self configDatas];
}

- (void)configDatas{

    [_imageV sd_setImageWithURL:[NSURL URLWithString:_goodsModel.goods_image] placeholderImage:[UIImage imageNamed:@"huibangke"]];
    _titleLab.text = _goodsModel.goods_name;
    _momeyLab.text = [NSString stringWithFormat:@"¥ %@", _goodsModel.goods_price];
}

- (void)setCollectionCell{

    _imageV = [[UIImageView alloc] init];
    [self.contentView addSubview:_imageV];
    
    _titleLab = [[UILabel alloc] init];
    [self.contentView addSubview:_titleLab];
    _titleLab.font = FTY_FONTTSIZE(15);
    _titleLab.numberOfLines = 2;
    [_titleLab sizeToFit];
    
    _momeyLab = [[UILabel alloc] init];
    [self.contentView addSubview:_momeyLab];
    _momeyLab.font = FTY_FONTTSIZE(18);
    _momeyLab.textColor = [FTYColor getMainColorWithAlpha:1.];
    
    _collectIcon = [[UIButton alloc] init];
    [self.contentView addSubview:_collectIcon];
    [_collectIcon addTarget:self action:@selector(collectIconAction:) forControlEvents:UIControlEventTouchUpInside];
    [_collectIcon setImage:[UIImage imageNamed:@"focus_sel_icon"] forState:UIControlStateNormal];
    
    _line = [[UIView alloc] init];
    [self.contentView addSubview:_line];
    _line.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
}

- (void)collectIconAction:(UIButton *)btn{

    if (_delegate && [_delegate respondsToSelector:@selector(collection:atIndex:)]) {
        [_delegate collection:btn atIndex:self.tag];
    }
}

- (void)layoutSubviews{

    [super layoutSubviews];
    WeakSelf(ws);
    
    [_imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10.*FTY_HEIGHT_BASE);
        make.left.mas_offset(10.*FTY_WIDTH_BASE);
        make.bottom.mas_equalTo(-10*FTY_HEIGHT_BASE);
        make.width.mas_offset(100.*FTY_WIDTH_BASE);
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(_imageV.mas_right).offset(10.*FTY_WIDTH_BASE);
        make.right.mas_offset(-60.*FTY_WIDTH_BASE);
    }];
    
    [_momeyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLab.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(_titleLab.mas_left);
    }];
    
    [_collectIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(ws.mas_centerY);
        make.right.mas_equalTo(-12.*FTY_WIDTH_BASE);
        make.width.height.mas_equalTo(30.*FTY_WIDTH_BASE);
    }];
    
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(1*FTY_HEIGHT_BASE); //思：此处设置为.5的高度，线不显示（）
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
